package leetcode_day._2021._202108._2131;

import java.util.Arrays;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/8/26 19:16
 * 救生艇
 * 算法：贪心  双指针
 * 将数组排序，从低到高
 * 由于船规定只能载两人，所以要每次最好载两人
 * 两个头尾指针
 */
public class _26_881 {

    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int ans = 0;
        int l = 0, r = people.length - 1;
        while (l <= r) {
            if (people[l] + people[r] <= limit) {
                ++l;
                --r;
            } else --r;
            ++ans;
        }
        return ans;
    }

}
